package com.luoqiu.cinema.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.luoqiu.cinema.common.PageParam;
import com.luoqiu.cinema.common.PageResult;
import com.luoqiu.cinema.common.Result;
import com.luoqiu.cinema.eitityExtends.BuyMovieParamEx;
import com.luoqiu.cinema.eitityExtends.EvaluateEx;
import com.luoqiu.cinema.entity.*;
import com.luoqiu.cinema.mapper.*;
import com.luoqiu.cinema.util.UserInfoUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class MovieService {

    @Resource
    private MovieEvaluateMapper movieEvaluateMapper;
    @Resource
    private UserInfoUtil userInfoUtil;
    @Resource
    private MovieMapper movieMapper;
    @Resource
    private MoviePlaceMovieMapper moviePlaceMovieMapper;
    @Resource
    private TicketUserMapper ticketUserMapper;
    @Resource
    private TicketMapper ticketMapper;
    @Resource
    private UserInfoMapper userInfoMapper;
    @Resource
    private TicketService ticketService;

    public Result<PageResult<EvaluateEx>> evaList(PageParam pageParam) {
        PageHelper.startPage(pageParam.getPage(), pageParam.getRows());
        List<EvaluateEx> evaluateExes = movieEvaluateMapper.loadByMovieId(pageParam.getId());
        PageInfo<EvaluateEx> pageInfo = new PageInfo<>(evaluateExes);
        PageResult<EvaluateEx> pageResult = new PageResult<>();
        pageResult.setData(evaluateExes);
        pageResult.setTotal(pageInfo.getTotal());
        return Result.success(pageResult);
    }

    public Result<String> add(MovieEvaluate movieEvaluate) {

        Integer id = movieEvaluate.getUid();
        movieEvaluate.setEvaluateId(id);
        movieEvaluate.setReleaseTime(new Date());
        movieEvaluateMapper.insertSelective(movieEvaluate);

        //增加积分
       // userInfoUtil.score(30);
        return Result.success(null);
    }

    public Result<String> addM(Movie movie) {
        if (movie.getId() == null) {
            movieMapper.insertSelective(movie);
        } else {
            movieMapper.updateByPrimaryKeySelective(movie);
        }
        return Result.success(null);
    }

    public Result<String> delM(Integer id) {
        movieMapper.deleteByPrimaryKey(id);
        movieEvaluateMapper.delByMovieId(id);
        moviePlaceMovieMapper.delByMovieId(id);
        ticketUserMapper.delByMovieId(id);
        ticketMapper.delByMovieId(id);
        return Result.success(null);
    }

    public Result<String> buyOther(BuyMovieParamEx paramEx) {
        UserInfo userInfo = userInfoMapper.loadByUsername(paramEx.getUsername());
        if (userInfo == null) {
            Result<String> fail = Result.fail("账号不存在");
            fail.setMsg("账号不存在");
            return fail;
        }
        ticketService.buyMovie(paramEx);
        return Result.success(null);
    }


    public Result<String> backOther(BuyMovieParamEx paramEx) {
        UserInfo userInfo = userInfoMapper.loadByUsername(paramEx.getUsername());
        if (userInfo == null) {
            Result<String> fail = Result.fail("账号不存在");
            fail.setMsg("账号不存在");
            return fail;
        }
        Integer movieId = paramEx.getMovieId();
        Integer placeId = paramEx.getPlaceId();
        TicketUser ticketUser = ticketUserMapper.loadByMovieIdAndPlaceId(movieId, placeId);
        Integer id = ticketUser.getId();
        ticketService.rollback(id);
        return Result.success(null);
    }
}
